﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hxj.Data;
using System.Data;
using Utility.Json;
using Utility;
using MsgCenter.DAL.Base;
using Utility.Model;

namespace MsgCenter.BLL.Impl
{
    /// <summary>
    /// 角色管理业务层
    /// </summary>
    public class BLL_SysRole
    {
        private readonly BaseService dal = new BaseService();
        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="model">对象实体</param>
        /// <param name="strSelectAuth">角色权限</param>
        public void Save(Sys_Role model, string strSelectAuth)
        {
            BLLServiceHelper.GetOperateService().SaveStartNull();
            DbTrans trans = dal.GetDataBase().BeginTransaction();
            try
            {
                string RoleID = model.RoleID;
                dal.Save<Sys_Role>(model, trans);
                dal.ExecuteNonQuery("delete Sys_RoleOperate where RoleID='" + RoleID + "'", trans);
                if (!strSelectAuth.IsNullOrEmpty())
                {
                    string[] strDataAuth = strSelectAuth.Split(',');
                    int DataAuth_Len = strDataAuth.Length;
                    if (DataAuth_Len > 0)
                    {
                        string[] sz1;
                        string ModuleID;
                        string OperateID;
                        foreach (string strAuth in strDataAuth)
                        {
                            sz1 = strAuth.Split('$');
                            if (sz1.Length > 1)
                            {
                                ModuleID = sz1[0].ToSafeString();
                                OperateID = sz1[1].ToSafeString();
                            }
                            else
                            {
                                ModuleID = sz1[0].ToSafeString();
                                OperateID = Guid.Empty.ToSafeString();
                            }
                            if (ModuleID != Guid.Empty.ToSafeString())
                            {
                                dal.ExecuteNonQuery("insert into Sys_RoleOperate(RoleID,ModuleID,OperateID)  values ('" + RoleID + "','" + ModuleID + "','" + OperateID + "')", trans);
                            }
                        }
                    }
                }
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
            finally
            {
                trans.Close();
            }
        }
        /// <summary>
        /// 删除,修改状态
        /// </summary>
        /// <param name="strDataID">数据ID</param>
        public void Delete(string strDataID)
        {
            if (strDataID.Length > 0)
            {
                string[] idList = strDataID.Trim(',').Split(',');
                DbTrans trans = dal.GetDataBase().BeginTransaction();
                try
                {
                    foreach (string strRoleID in idList)
                    {
                        dal.ExecuteNonQuery("Update Sys_Role set Isdelete=1 where RoleID='" + strRoleID + "'", trans);
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
                finally
                {
                    trans.Close();
                }
            }
        }
        /// <summary>
        /// 获取对象实体
        /// </summary>
        /// <param name="id">数据ID</param>
        /// <returns>Sys_Role</returns>
        public Sys_Role GetModel(string id)
        {
            return dal.GetModel<Sys_Role>(Sys_Role._.RoleID == id);
        }
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="wcp">WhereClip条件</param>
        /// <returns>DataTable</returns>
        public DataTable GetRoleTable(WhereClip wcp)
        {
            return dal.GetTable<Sys_Role>(wcp);
        }
        /// <summary>
        /// 获取角色权限,创建树
        /// </summary>
        /// <param name="RoleID">角色ID</param>
        /// <returns>DataSet</returns>
        public DataSet GetAllList(string RoleID)
        {
            string strSql = "select *  FROM Sys_Module where isdelete=0 order by fatherid,sort";
            strSql += " SELECT  b.RoleID, a.ModuleID, a.ModuleName,a.FatherID, a.LinkAddress, a.sort, a.ID,a.IsDelete, a.CssCss FROM Sys_Module AS a INNER JOIN Sys_RoleOperate AS b ON a.ID = b.ModuleID AND b.OperateID='00000000-0000-0000-0000-000000000000' WHERE (a.IsDelete = 0) and b.RoleID='" + RoleID + "'";
            strSql += " SELECT a.ModuleID, a.OperateID, b.OperateName, b.OperateValue, b.IsDelete, b.Createdate FROM Sys_ModuleOperate AS a INNER JOIN Sys_Operate AS b ON a.OperateID = b.OperateID WHERE     (b.IsDelete = 0)";
            strSql += " SELECT     a.RoleID, a.ModuleID, a.OperateID, b.OperateName, b.Createdate, b.IsDelete, b.OperateValue FROM Sys_RoleOperate AS a INNER JOIN Sys_Operate AS b ON a.OperateID = b.OperateID AND a.OperateID<>'00000000-0000-0000-0000-000000000000' WHERE (b.IsDelete = 0) and a.RoleID='" + RoleID + "'";
            return dal.GetDataBase().FromSql(strSql).ToDataSet();
        }

        /// <summary>
        /// 删除,修改状态
        /// </summary>
        /// <param name="strDataID">数据ID</param>
        public void DeleteTemp(string strDataID)
        {
            if (strDataID.Length > 0)
            {
                string[] idList = strDataID.Trim(',').Split(',');
                DbTrans trans = dal.GetDataBase().BeginTransaction();
                try
                {
                    foreach (string strTempID in idList)
                    {
                        dal.ExecuteNonQuery("Update Sys_Templet set Isdelete=1 where TempID='" + strTempID + "'", trans);
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
                finally
                {
                    trans.Close();
                }
            }
        }
        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="model">对象实体</param>
        /// <param name="strSelectAuth">角色权限</param>
        public void Save(Sys_Templet model)
        {
            DbTrans trans = dal.GetDataBase().BeginTransaction();
            try
            {
                dal.Save<Sys_Templet>(model, trans);
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
            finally
            {
                trans.Close();
            }
        }
    }
}
